package controller;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;

import data.Data;

@SuppressWarnings("serial")
public class CheckCouponCodeServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		
		response.setContentType("text/html");
		HttpSession session = request.getSession();
		DBObject dbObject1 = (DBObject) session.getAttribute("users");
		if(dbObject1==null)
		{
			try {
				response.getWriter().println("<script>window.location='http://hatforrent.com/login'</script>");
				return;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		response.setContentType("text/html");
		String coupon = request.getParameter("coupon");
		
		DBCollection collection = Data.data("coupon");
		DBObject dbObject = collection.findOne(new BasicDBObject("coupon",coupon));
		if(dbObject!=null)
		{
			response.getWriter().println(dbObject.get("day"));
		} else {
			response.getWriter().println("2");
		}
	}
}
